using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.OleDb;
using System.Data.SqlClient;


namespace Module.Core.Member
{
    /// <summary>
    /// Summary description for DBUtilEx.
    /// </summary>
    #region "Ket noi va lam viec voi DataBase"
    public class DBUtilEx
    {

        public static OleDbConnection OpenConnectionFOX(string cnnStr)
        {
            try
            {
                OleDbConnection cnn = new OleDbConnection(cnnStr);
                cnn.Open();
                return cnn;
            }
            catch (Exception e)
            {
                throw (new Exception(e.Message));
            }
        }
        public static SqlConnection OpenConnectionSQL(string cnnStr)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(cnnStr);
                cnn.Open();
                return cnn;
            }
            catch (Exception e)
            {
                throw (new Exception(e.Message));
            }
        }
        public static void CloseConnectionSQL(SqlConnection cnn)
        {
            try
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            catch (Exception e)
            {
                throw (new Exception(e.Message));
            }
        }


        public static void executeNoneQuerySQL(SqlConnection cnn, string strSQL)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(strSQL, cnn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {

            }
        }

        public static void executeNoneQuery(SqlConnection cnn, string strSQL)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(strSQL, cnn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw (new Exception(ex.Message + "=>" + strSQL));
            }
        }


        public static DataTable getDataFromSQLServer(SqlConnection cnn, string strSQL)
        {
            try
            {

                SqlDataAdapter dataAdapter = new SqlDataAdapter();
                dataAdapter.SelectCommand = new SqlCommand(strSQL, cnn);
                DataSet ds = new DataSet();
                dataAdapter.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception e)
            {
                throw (new Exception(e.Message));
            }
        }

        public static DataTable RunSQL(string strSQL, SqlConnection cnn)
        {
            try
            {
                SqlDataAdapter empAdapter = new SqlDataAdapter();
                empAdapter.SelectCommand = new SqlCommand(strSQL, cnn);
                DataSet ds = new DataSet();
                empAdapter.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception e)
            {
                throw (new Exception(e.Message));
            }
        }

    }
    #endregion
}

